/*
 *	Copyright © 2013 Changsha Shishuo Network Technology Co., Ltd. All rights reserved.
 *	长沙市师说网络科技有限公司 版权所有
 *	http://www.shishuo.com
 */
package com.shishuo.cms.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;

import com.shishuo.cms.entity.Folder;
import com.shishuo.cms.entity.vo.FolderVo;
import com.shishuo.cms.entity.vo.JsonVo;
import com.shishuo.cms.exception.FolderNotFoundException;
import com.shishuo.cms.exception.ValidateException;
import com.shishuo.cms.service.ArticleCommentService;
import com.shishuo.cms.service.ArticleService;
import com.shishuo.cms.service.AttachmentService;
import com.shishuo.cms.service.ConfigService;
import com.shishuo.cms.service.FolderService;
import com.shishuo.cms.service.TemplateService;

/**
 * 
 * @author Herbert
 * 
 */
public class BaseAction {

	@Autowired
	protected FolderService folderService;

	@Autowired
	protected ArticleService fileService;
	@Autowired
	protected AttachmentService attachmentService;
	
	
	@Autowired
	protected ArticleCommentService articleCommentService;
	
	@Autowired
	protected ConfigService configService;

	@Autowired
	protected TemplateService themeService;

	protected final Logger logger = Logger.getLogger(this.getClass());

	/**
	 * 参数校验
	 * 
	 * @param json
	 *            json数据Bean
	 * @throws ValidateException
	 */
	protected <T> void validate(JsonVo<T> json) throws ValidateException {
		if (json.getErrors().size() > 0) {
			json.setResult(false);
			throw new ValidateException("有错误发生");
		} else {
			json.setResult(true);
		}
	}

	/**
	 * 包装返回给页面的各级Folder数据
	 * 
	 * @param firstFolderEname
	 * @param secondFolderEname
	 * @param thirdFolderEname
	 * @param fourthFolderEname
	 * @param modelMap
	 * @return
	 * @throws FolderNotFoundException
	 */
	protected List<Folder> packageFolderByEname(String firstFolderEname,
			String secondFolderEname, String thirdFolderEname,
			String fourthFolderEname, ModelMap modelMap)
			throws FolderNotFoundException {
		List<Folder> folderPathList = new ArrayList<Folder>();
		FolderVo firstFolder = folderService.getFolderByEnameAndFatherId(
				firstFolderEname, 0);
		modelMap.addAttribute("firstFolder", firstFolder);
		modelMap.addAttribute("folder", firstFolder);
		folderPathList.add(firstFolder);
		if (StringUtils.isNotBlank(secondFolderEname)) {
			FolderVo secondFolder = folderService.getFolderByEnameAndFatherId(
					secondFolderEname, firstFolder.getFolderId());
			modelMap.addAttribute("secondFolder", secondFolder);
			modelMap.addAttribute("folder", secondFolder);
			folderPathList.add(secondFolder);
			if (StringUtils.isNotBlank(thirdFolderEname)) {
				FolderVo thirdFolder = folderService
						.getFolderByEnameAndFatherId(thirdFolderEname,
								secondFolder.getFolderId());
				modelMap.addAttribute("thirdFolder", thirdFolder);
				modelMap.addAttribute("folder", thirdFolder);
				folderPathList.add(thirdFolder);
				if (StringUtils.isNotBlank(fourthFolderEname)) {
					FolderVo fourthFolder = folderService
							.getFolderByEnameAndFatherId(fourthFolderEname,
									secondFolder.getFolderId());
					modelMap.addAttribute("fourthFolder", fourthFolder);
					modelMap.addAttribute("folder", fourthFolder);
					folderPathList.add(fourthFolder);
				}
			}
		}
		modelMap.addAttribute("folderPathList", folderPathList);
		return folderPathList;
	}
	
	
	protected List<Folder> packageFolderByEname(String name, ModelMap modelMap)
			throws FolderNotFoundException {
		List<Folder> folderPathList = new ArrayList<Folder>();
		FolderVo folder = folderService.getFirstFolderByName(name);
		String firstFolderEname=null;
		String secondFolderEname=null; 
		String thirdFolderEname=null;
		String fourthFolderEname=null;
		//获取上级文件夹名称
		if (null!=folder) {
			folderPathList.add(folder);
			String path= folder.getPath();
			String[] folderIds=path.split("#");
			for (int i = 0; i < folderIds.length; i++) {
				switch (i) {
					case 0:
						firstFolderEname=folderService.getFolderById(Long.parseLong(folderIds[i])).getEname();
						break;
					case 1:
						secondFolderEname=folderService.getFolderById(Long.parseLong(folderIds[i])).getEname();
						break;
					case 2:
						thirdFolderEname=folderService.getFolderById(Long.parseLong(folderIds[i])).getEname();
						break;
					case 3:
						fourthFolderEname=folderService.getFolderById(Long.parseLong(folderIds[i])).getEname();
						break;
					default:
						break;
				}
			}
			return packageFolderByEname(firstFolderEname,secondFolderEname,thirdFolderEname,fourthFolderEname,modelMap);
		}
		throw new FolderNotFoundException(name + " 目录，不存在");
	}
	
	/**
	 * 获取访问者IP地址
	 * @param request
	 * @return
	 */
	public String getIpAddr(HttpServletRequest request) {  
	    String ip = request.getHeader("x-forwarded-for");  
	    if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
	        ip = request.getHeader("Proxy-Client-IP");  
	    }  
	    if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
	        ip = request.getHeader("WL-Proxy-Client-IP");  
	    }  
	    if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
	        ip = request.getRemoteAddr();  
	    }  
	    return ip;  
	}  
}
